<template>
    <div id="div">
        <el-row :gutter="20">
            <el-col :span="17">
                <div id="Container" class="x6-graph"/>
                <div id="Stencil" class="sider"/>
            </el-col>
            <el-col :span="7">
                <el-form label-width="140px">
                    <el-form-item label="选择故障树：">
                        <el-select v-model="faultTree" placeholder="请选择" @change="changeTree">
                            <el-option
                                    v-for="item in faultTreeData"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.name">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="选择故障树事件：">
                        {{ this.celllabel }}
                    </el-form-item>
                    <el-form-item label="操作：">
                        <el-tooltip effect="dark" content="定性分析" placement="top" :enterable="false">
                            <el-button type="primary" size="small" @click="targetJson">定性分析</el-button>
                        </el-tooltip>
                        <el-tooltip effect="dark" content="定量分析" placement="top" :enterable="false">
                            <el-button type="primary" size="small" @click="analyseJson">定量分析</el-button>
                        </el-tooltip>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <!--显示概率重要度对话框-->
        <el-dialog title="概率重要度" :visible.sync="importantDialogVisible" width="50%" @close="importantDialogClosed">
            <el-table
                :data="importantData"
                border
                height="600"
                style="width: 100%"
                :default-sort = "{prop: 'importantnum', order: 'descending'}"
            >
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="target" label="底事件名称" min-width="30%"></el-table-column>
                <el-table-column prop="probability" label="发生概率" min-width="30%"></el-table-column>
                <el-table-column prop="importantnum" label="概率重要度" sortable min-width="30%"></el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button @click="importantDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="importantDialogInfo">确 定</el-button>
            </span>
        </el-dialog>
        <!--定性分析对话框-->
        <el-dialog title="最小割集" :visible.sync="targetDialogVisible" width="50%" @close="targetDialogClosed">
            <el-table
                :data="targetData"
                border
                height="600"
                style="width: 100%"
                :default-sort = "{prop: 'probability', order: 'descending'}"
            >
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="target" label="最小割集" min-width="70%"></el-table-column>
                <el-table-column prop="probability" label="发生概率" sortable min-width="30%"></el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button @click="targetDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="targetDialogInfo">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import FlowGraph from "../views/graph"
    export default {
        name: "fault",
        data(){
            return{
                // 获取的故障树数据
                faultTreeData: [],
                // 当前的故障树数据
                currentFaultTree: [],
                faultTree: "",
                importantData: [{}],
                // 存放边的数据
                edgeData:[],
                // 存放块的数据
                rectData:[],
                // 存放块中文本相关的数据
                textData:[],
                textObj:{
                    "source": "",
                    "sourceId": "",
                    "target": "",
                    "targetId": ""
                },
                // 定性分析结果
                resultData:[],
                curData: [],

                curRect: "",
                curEdge: "",
                curEdgeId: "",

                tmpArray:[],
                // 与门
                curAnd: 0,
                readyAnd: 0,
                tmpM: 0,
                markAnd: 0,
                // 或门
                curOr: 0,
                readyOr: 0,
                tmpOr:[],
                markOr: 0,
                // 非门
                markNo: 0,
                // 转移门
                transferNum: 0,
                transferRect: '',

                probabilityData: [],
                analyseData:[],

                cellid: "",
                celllabel: "",

                importantDialogVisible: false,
                targetDialogVisible:false,

                targetData: [],
            }
        },
        mounted(){
            this.initGraph()
            this.getJson()
            setTimeout(() => {
                this.boundEvent()
            }, 200)
        },
        methods:{
             // 初始化故障树
            initGraph(){
                const graph = FlowGraph.init()
                // 适应页面大小
                const resizeFn = () => {
                    const {width, height} = this.getContainerSize()
                    graph.resize(width, height)
                }
                resizeFn()
                window.addEventListener('resize', resizeFn)
                return () => {
                    window.removeEventListener('resize', resizeFn)
                }
            },
            getContainerSize() {
                let div = document.getElementById("app")
                return {
                    width: div.offsetWidth -600 ,
                    height: div.offsetHeight - 137
                }
            },
            boundEvent() {
                const {graph} = FlowGraph
                // 点击空白处
                graph.on('blank:click', () => {
                    this.type = 'grid'
                })
                // 点击cell判断是node还是edge
                graph.on('cell:click', ({cell}) => {
                    if (cell.isNode()) {
                        this.cellid = cell.id
                        if (cell.label === undefined) {
                            this.celllabel = cell.attrs.text.textWrap.text
                            this.change()
                        } else {
                            this.celllabel = cell.label
                            this.change()
                        }
                    }
                })
            },
            async getJson() {
                console.log(this.$route.query.tree)
                const res = await this.$http.get('/knowledge_fault_tree/fault_tree/');
                if (res.status !== 200) return this.$message.error('故障树数据获取失败！')
                this.$message.success('获取故障树成功！')
                this.faultTreeData = res.data;
                if(this.$route.query.tree !== undefined){
                    this.faultTree = this.$route.query.tree
                    this.change()
                    this.changeTree()
                }

            },
            changeTree(){
                let name = this.faultTree
                this.currentFaultTree = this.faultTreeData.map(function (item) {
                    if(item.name === name){
                        return item
                    }
                });
                this.currentFaultTree = this.currentFaultTree.filter(function (e) {
                    return e
                });
                console.log(this.currentFaultTree[0])
                const {graph} = FlowGraph
                graph.clearCells()
                graph.fromJSON(this.currentFaultTree[0].json)
            },
            // 定性分析代码
            testJson(){
                console.log(this.currentFaultTree)
                // edge需要用到ID sourceID targetID
                this.edgeData = this.currentFaultTree[0].json.cells.map(function (item) {
                    if(item.shape === "edge"){
                        return item
                    }
                });
                this.edgeData = this.edgeData.filter(function (e) {
                    return e
                });
                console.log(this.edgeData)
                // rect需要用到ID text shape
                this.rectData = this.currentFaultTree[0].json.cells.map(function (item) {
                    if(item.shape !== "edge"){
                        return item
                    }
                });
                this.rectData = this.rectData.filter(function (e) {
                    return e
                });
                console.log(this.rectData)
                // 操作前先重置数组
                this.textData = []
                this.resultData = []
                this.tmpArray = []
                // 遍历edge去rect中匹配对应的块,获取text
                for(let i=0; i<this.edgeData.length;i++){
                    for(let j=0; j<this.rectData.length; j++){
                        switch (this.rectData[j].shape) {
                            case "flow-chart-rect":
                                if (this.edgeData[i].source.cell === this.rectData[j].id) {
                                    this.textObj.source = this.rectData[j].attrs.text.textWrap.text
                                    this.textObj.sourceId = this.rectData[j].id
                                }
                                if (this.edgeData[i].target.cell === this.rectData[j].id) {
                                    this.textObj.target = this.rectData[j].attrs.text.textWrap.text
                                    this.textObj.targetId = this.rectData[j].id
                                    if(this.textObj.target === "非门"){
                                        this.textObj.shape = "门"
                                    }
                                }
                                break
                            case "flow-chart-image1":
                                if (this.edgeData[i].source.cell === this.rectData[j].id) {
                                    this.textObj.source = "与门"
                                    this.textObj.sourceId = this.rectData[j].id
                                }
                                if (this.edgeData[i].target.cell === this.rectData[j].id) {
                                    this.textObj.target = "与门"
                                    this.textObj.targetId = this.rectData[j].id
                                    this.textObj.shape = "门"
                                }
                                break
                            case "flow-chart-image2":
                                if (this.edgeData[i].source.cell === this.rectData[j].id) {
                                    this.textObj.source = "或门"
                                    this.textObj.sourceId = this.rectData[j].id
                                }
                                if (this.edgeData[i].target.cell === this.rectData[j].id) {
                                    this.textObj.target = "或门"
                                    this.textObj.targetId = this.rectData[j].id
                                    this.textObj.shape = "门"
                                }
                                break
                            case "flow-chart-image3":
                                if (this.edgeData[i].source.cell === this.rectData[j].id) {
                                    this.textObj.source = "转移门"
                                    this.textObj.sourceId = this.rectData[j].id
                                }
                                if (this.edgeData[i].target.cell === this.rectData[j].id) {
                                    this.textObj.target = "转移门"
                                    this.textObj.targetId = this.rectData[j].id
                                    this.textObj.shape = "门"
                                }
                                break
                            case "flow-chart-image4":
                                if (this.edgeData[i].source.cell === this.rectData[j].id) {
                                    this.textObj.source = "禁门"
                                    this.textObj.sourceId = this.rectData[j].id
                                }
                                if (this.edgeData[i].target.cell === this.rectData[j].id) {
                                    this.textObj.target = "禁门"
                                    this.textObj.targetId = this.rectData[j].id
                                    this.textObj.shape = "门"
                                }
                                break
                            default:
                                break

                        }
                    }
                    this.textData.push(this.textObj)
                    this.textObj = {
                        "source": "",
                        "sourceId": "",
                        "target": "",
                        "targetId": "",
                        "shape":""
                    }
                }
                console.log(JSON.parse(JSON.stringify(this.textData)))
                this.sortJson()
                for(let k=0; k<this.textData.length; k++) {
                    // 判断是哪种类型，块-门、门-块、块-块
                    console.log(this.dataType(this.textData[k]))
                    switch (this.dataType(this.textData[k])) {
                        case 1:
                            this.curAnd = 0
                            this.readyAnd = 0
                            this.tmpM = ""
                            this.tmpOr = []
                            this.curOr = 0
                            this.readyOr = 0
                            // 如果resultData为空，增加第一个顶事件到数组中
                            if (this.resultData.length === 0) {
                                let tmpData = {
                                    target: this.textData[k].target,
                                    targetId: this.textData[k].targetId
                                }
                                this.tmpArray.push(tmpData)
                                this.resultData.push(this.tmpArray)
                            } else {
                                // 块-门也进行替换，将事件替换成门，ID也替换
                                console.log(JSON.parse(JSON.stringify(this.resultData)))
                                console.log("块-门替换" + k)
                                for (let n = 0; n < this.resultData.length; n++) {
                                    for (let m = 0; m < this.resultData[n].length; m++) {
                                        if (this.textData[k].sourceId === this.resultData[n][m].targetId) {
                                            this.resultData[n][m].target = this.textData[k].target
                                            this.resultData[n][m].targetId = this.textData[k].targetId
                                        }
                                    }
                                }
                            }
                            console.log(JSON.parse(JSON.stringify(this.resultData)))
                            // 转移门的情况(转移门属于块-门)
                            if (this.textData[k].target === "转移门") {
                                if (this.transferNum === 0) {
                                    console.log("转移门第一次")
                                    this.transferNum = 1
                                }
                            }
                            // 保存未发生改变前的resultData进行循环
                            this.curData = JSON.parse(JSON.stringify(this.resultData))
                            break
                        case 2:
                            // 应该遍历数组找到对应的块，进行与门——替换，或门——替换并新增
                            this.breakId = 1
                            for (let i = 0; i < this.curData.length; i++) {
                                for (let j = 0; j < this.curData[i].length; j++) {
                                    if (this.textData[k].sourceId === this.curData[i][j].targetId) {
                                        this.curEdge = this.curData[i][j].target
                                        this.breakId = 0
                                        break
                                    }
                                    if (this.textData[k].source === "转移门" && this.transferNum === 1) {
                                        this.curEdge = "转移门"
                                        this.breakId = 0
                                        break
                                    }
                                }
                                if (this.breakId === 0) break
                            }
                            // 与门（禁门）的情况
                            if (this.curEdge === "与门" || this.curEdge === "禁门") {
                                for (let n = 0; n < this.curData.length; n++) {
                                    for (let m = 0; m < this.curData[n].length; m++) {
                                        // 第n个数组中存在curRect
                                        if (this.curData[n][m].targetId === this.textData[k].sourceId) {
                                            console.log("与门存在curRect")
                                            // 由于重排的问题，会出现直接push没有change，应该标记
                                            if (this.textData[k].sourceId !== this.curRect) {
                                                console.log("重新change")
                                                this.readyAnd = 0
                                            }
                                            this.curAnd = 1
                                            this.tmpM = m
                                            this.markAnd = 0
                                            break
                                        } else this.markAnd = 1
                                    }
                                    // 如果第n个数组和要处理的与门无关，应该continue
                                    if (this.markAnd === 1) {
                                        this.markAnd = 0
                                        console.log("continue!!")
                                        continue
                                    }
                                    // 第n个数组第一次替换
                                    if (this.curAnd === 1 && this.readyAnd === 0) {
                                        this.resultData[n][this.tmpM].target = this.textData[k].target
                                        this.resultData[n][this.tmpM].targetId = this.textData[k].targetId
                                        console.log("change!!")
                                    }// 只有这一个数组有替换过才能接着进行push
                                    else if (this.curAnd === 1 && this.readyAnd === 1) {
                                        let tmpData = {
                                            target: this.textData[k].target,
                                            targetId: this.textData[k].targetId
                                        }
                                        this.resultData[n].push(tmpData)
                                        console.log("push!!")
                                    }
                                }
                                // 第一次替换完成后readyAnd为1，下一次将会进行push
                                if (this.curAnd === 1 && this.readyAnd === 0) {
                                    this.readyAnd = 1
                                    this.curRect = this.textData[k].sourceId
                                }
                            }
                            // 或门的情况
                            if (this.curEdge === "或门") {
                                for (let n = 0; n < this.curData.length; n++) {
                                    for (let m = 0; m < this.curData[n].length; m++) {
                                        // 第n个数组中存在curRect
                                        if (this.curData[n][m].targetId === this.textData[k].sourceId) {
                                            console.log("或门存在curRect")
                                            // 由于重排的问题，会出现直接push没有change，应该标记
                                            if (this.textData[k].sourceId !== this.curRect) {
                                                console.log("重新change")
                                                this.readyOr = 0
                                            }
                                            this.curOr = 1
                                            this.tmpM = m
                                            this.markOr = 0
                                            break
                                        } else this.markOr = 1
                                    }
                                    // 如果第n个数组和要处理的或门无关，应该continue
                                    if (this.markOr === 1) {
                                        this.markOr = 0
                                        console.log("continue!")
                                        continue
                                    }
                                    // 或门都会使数组数量增加
                                    if (this.curOr === 1 && this.readyOr === 0) {
                                        console.log("change!")
                                        this.resultData[n][this.tmpM].target = this.textData[k].target
                                        this.resultData[n][this.tmpM].targetId = this.textData[k].targetId
                                    } else if (this.curOr === 1 && this.readyOr === 1) {
                                        console.log("push!")
                                        this.tmpOr = JSON.parse(JSON.stringify(this.resultData[n]))
                                        this.tmpOr[this.tmpM].target = this.textData[k].target
                                        this.tmpOr[this.tmpM].targetId = this.textData[k].targetId
                                        console.log(JSON.parse(JSON.stringify(this.tmpOr)))
                                        this.resultData.push(this.tmpOr)
                                    }
                                }
                                // 第一次替换完成后readyOr为1，下一次将会进行push
                                if (this.curOr === 1 && this.readyOr === 0) {
                                    this.readyOr = 1
                                    this.curRect = this.textData[k].sourceId
                                }
                            }
                            // 非门的情况（非门只要进行一次替换就可以）
                            if (this.curEdge === "非门") {
                                for (let n = 0; n < this.curData.length; n++) {
                                    for (let m = 0; m < this.curData[n].length; m++) {
                                        // 第n个数组中存在curRect
                                        if (this.curData[n][m].targetId === this.textData[k].sourceId) {
                                            console.log("非门存在curRect")
                                            this.tmpM = m
                                            this.markNo = 0
                                            break
                                        } else this.markNo = 1
                                    }
                                    // 如果第n个数组和要处理的或门无关，应该continue
                                    if (this.markNo === 1) {
                                        this.markNo = 0
                                        console.log("continue!")
                                        continue
                                    }
                                    // 非门直接进行替换
                                    console.log("非门change")
                                    this.resultData[n][this.tmpM].target = this.textData[k].target
                                    this.resultData[n][this.tmpM].targetId = this.textData[k].targetId
                                }
                            }
                            console.log(JSON.parse(JSON.stringify(this.resultData)))
                            // 转移门的情况（直接替换）
                            if (this.curEdge === "转移门" && this.transferNum === 1) {
                                console.log("转移门替换")
                                for (let n = 0; n < this.resultData.length; n++) {
                                    for (let m = 0; m < this.resultData[n].length; m++) {
                                        if (this.resultData[n][m].target === "转移门") {
                                            this.resultData[n][m].target = this.textData[k].target
                                            this.resultData[n][m].targetId = this.textData[k].targetId
                                        }
                                    }
                                }
                                this.transferNum = 0
                            }
                            console.log(JSON.parse(JSON.stringify(this.resultData)))
                            break
                        case 3:
                            // 都是块直接更新对应块就行
                            console.log("块-块替换" + k)
                            for (let n = 0; n < this.resultData.length; n++) {
                                for (let m = 0; m < this.resultData[n].length; m++) {
                                    if (this.textData[k].sourceId === this.resultData[n][m].targetId) {
                                        this.resultData[n][m].target = this.textData[k].target
                                        this.resultData[n][m].targetId = this.textData[k].targetId
                                    }
                                }
                            }
                            console.log(JSON.parse(JSON.stringify(this.resultData)))
                            break
                        default:
                            break
                    }
                }
            },
            async targetJson(){
                this.testJson()
                // 获取数据库的概率数据
                let res = await this.$http.get('/fault_tree/probability/')
                let id = this.currentFaultTree[0].id
                res.data = res.data.map(function (item) {
                    if (item.tree === id) {
                        return item
                    }
                });
                res.data = res.data.filter(function (e) {
                    return e
                });
                console.log(res.data)
                for (let i = 0; i < this.resultData.length; i++) {
                    this.resultData[i][0].probability = 1
                    for (let j = 0; j < this.resultData[i].length; j++) {
                        for (let n = 0; n < res.data.length; n++) {
                            if (this.resultData[i][j].targetId === res.data[n].targetid) {
                                this.resultData[i][0].probability *= res.data[n].probability
                                break
                            }
                        }
                    }
                }
                console.log(JSON.parse(JSON.stringify(this.resultData)))
                this.targetData = []
                for (let i = 0; i < this.resultData.length; i++) {
                    let obj = {
                        'probability': null,
                        'target': ''
                    }
                    for (let j = 0; j < this.resultData[i].length; j++) {
                        obj.target = obj.target + this.resultData[i][j].target + ','
                    }
                    obj.probability = this.resultData[i][0].probability
                    this.targetData.push(obj)
                }
                console.log(JSON.parse(JSON.stringify(this.targetData)))
                this.targetDialogVisible = true
            },
            // 对故障树的顺序进行重排，以符合定性、定量的要求
            sortJson(){
                console.log(JSON.parse(JSON.stringify(this.textData)))
                console.log(this.currentFaultTree[0].name)
                console.log("重新排序")
                // 先找到顶事件，判断source
                for(let i=0;i<this.textData.length;i++){
                    if(this.textData[i].source === this.currentFaultTree[0].name || this.textData[i].source === "顶事件"){
                        [this.textData[0] , this.textData[i]] = [this.textData[i] , this.textData[0]]
                        console.log("第"+ i + "个事件为顶事件")
                        break
                    }
                }
                // 记录顶事件所对应的targetId
                let targetId = this.textData[0].targetId
                // sortNum存储顶事件所对应的第一层子事件
                let sortNum = 1
                for (let j = sortNum; j < this.textData.length; j++) {
                    if (this.textData[j].sourceId === targetId) {
                        [this.textData[sortNum], this.textData[j]] = [this.textData[j], this.textData[sortNum]]
                        console.log("第一层子事件确定" + sortNum)
                        console.log(this.textData[sortNum].target)
                        sortNum++
                    }
                }
                console.log(sortNum)
                // 从不是第一层子事件的下一个开始，遍历分别找到第一层子事件下面所有子树
                let beginNum = sortNum
                let endNum = sortNum
                for(let n=1;n<sortNum;n++){
                    // 找到第一层子事件下的下一个事件（可能是门、直接连接块、为空）
                    for(let m= endNum;m<this.textData.length;m++){
                        if(this.textData[m].sourceId === this.textData[n].targetId){
                            [this.textData[endNum], this.textData[m]] = [this.textData[m], this.textData[endNum]]
                            console.log(this.textData[endNum].target)
                            endNum++
                            console.log("beginNum:"+beginNum+",endNum:"+endNum)
                        }
                    }
                    if(endNum>beginNum){
                        if(this.textData[beginNum].shape === "门"){
                            console.log("第一层子事件的下一个事件为门")
                        }else {
                            console.log("第一层子事件的下一个事件为块")
                        }
                    }else if(endNum === beginNum){
                        console.log("下一个事件为空")
                        continue
                    }
                    while (endNum > beginNum) {
                        if (this.textData[beginNum].shape === "门") {
                            for (let m = endNum; m < this.textData.length; m++) {
                                if (this.textData[m].sourceId === this.textData[beginNum].targetId) {
                                    [this.textData[endNum], this.textData[m]] = [this.textData[m], this.textData[endNum]]
                                    console.log(this.textData[endNum].target)
                                    endNum++
                                    console.log("门：beginNum:"+beginNum+",endNum++:"+endNum)
                                }
                            }
                            beginNum++
                            console.log("门：beginNum++:"+beginNum+",endNum:"+endNum)
                        }else if(this.textData[beginNum].shape === ""){
                            for (let m = endNum; m < this.textData.length; m++) {
                                if (this.textData[m].sourceId === this.textData[beginNum].targetId) {
                                    [this.textData[endNum], this.textData[m]] = [this.textData[m], this.textData[endNum]]
                                    console.log(this.textData[endNum].target)
                                    endNum++
                                    console.log("块：beginNum:"+beginNum+",endNum++:"+endNum)
                                }
                            }
                            beginNum++
                            console.log("块：beginNum++:"+beginNum+",endNum:"+endNum)
                        }
                    }
                    beginNum = endNum
                }
                // 转移门
                if(beginNum<this.textData.length-1){
                    console.log("转移门重排")
                    // 先找到转移门
                    for (let i = beginNum; i < this.textData.length; i++) {
                        if (this.textData[i].source === "转移门") {
                            [this.textData[beginNum], this.textData[i]] = [this.textData[i], this.textData[beginNum]]
                            console.log("第" + i + "个事件为转移门")
                            break
                        }
                    }
                    // 记录顶事件所对应的targetId
                    let targetId = this.textData[beginNum].targetId
                    sortNum = 1
                    beginNum += 1
                    for (let j = beginNum; j < this.textData.length; j++) {
                        if (this.textData[j].sourceId === targetId) {
                            [this.textData[beginNum], this.textData[j]] = [this.textData[j], this.textData[beginNum]]
                            console.log("转移门子事件确定" + beginNum)
                            console.log(this.textData[beginNum].target)
                            beginNum++
                            sortNum++
                        }
                    }
                    console.log(beginNum)
                    endNum = beginNum
                    for (let n = 1; n < sortNum; n++) {
                        // 找到转移门的下一个事件（可能是门、直接连接块、为空）
                        for (let m = endNum; m < this.textData.length; m++) {
                            if (this.textData[m].sourceId === this.textData[beginNum-1].targetId) {
                                [this.textData[endNum], this.textData[m]] = [this.textData[m], this.textData[endNum]]
                                console.log(this.textData[endNum].target)
                                endNum++
                                console.log("beginNum:" + beginNum + ",endNum:" + endNum)
                            }
                        }
                        if (endNum > beginNum) {
                            if (this.textData[beginNum].shape === "门") {
                                console.log("转移门的下一个事件为门")
                            } else {
                                console.log("转移门的下一个事件为块")
                            }
                        } else if (endNum === beginNum) {
                            console.log("下一个事件为空")
                            continue
                        }
                        while (endNum > beginNum) {
                            if (this.textData[beginNum].shape === "门") {
                                for (let m = endNum; m < this.textData.length; m++) {
                                    if (this.textData[m].sourceId === this.textData[beginNum].targetId) {
                                        [this.textData[endNum], this.textData[m]] = [this.textData[m], this.textData[endNum]]
                                        console.log(this.textData[endNum].target)
                                        endNum++
                                        console.log("门：beginNum:" + beginNum + ",endNum++:" + endNum)
                                    }
                                }
                                beginNum++
                                console.log("门：beginNum++:" + beginNum + ",endNum:" + endNum)
                            } else if (this.textData[beginNum].shape === "") {
                                for (let m = endNum; m < this.textData.length; m++) {
                                    if (this.textData[m].sourceId === this.textData[beginNum].targetId) {
                                        [this.textData[endNum], this.textData[m]] = [this.textData[m], this.textData[endNum]]
                                        console.log(this.textData[endNum].target)
                                        endNum++
                                        console.log("块：beginNum:" + beginNum + ",endNum++:" + endNum)
                                    }
                                }
                                beginNum++
                                console.log("块：beginNum++:" + beginNum + ",endNum:" + endNum)
                            }
                        }
                        beginNum = endNum
                    }
                }
                console.log("重排结束")
                console.log(JSON.parse(JSON.stringify(this.textData)))
            },
            // 定量分析代码
            async analyseJson(){
                this.testJson()
                console.log(this.cellid)
                console.log(this.celllabel)
                console.log(JSON.parse(JSON.stringify(this.textData)))
                console.log(this.currentFaultTree[0].id)
                this.analyseData = []
                // 从textData中找出与选中块相关的所有数据
                for(let i=0;i<this.textData.length;i++){
                    if(this.textData[i].sourceId === this.cellid){
                        this.analyseData.push(this.textData[i])
                        continue
                    }
                    for(let j=0;j<this.analyseData.length;j++){
                        if(this.textData[i].sourceId === this.analyseData[j].targetId){
                            this.analyseData.push(this.textData[i])
                            break
                        }
                    }
                }
                console.log(JSON.parse(JSON.stringify(this.analyseData)))
                // 获取数据库的概率数据
                let res = await this.$http.get('/fault_tree/probability/')
                let id = this.currentFaultTree[0].id
                res.data = res.data.map(function (item) {
                    if(item.tree === id){
                        return item
                    }
                });
                res.data = res.data.filter(function (e) {
                    return e
                });
                console.log(res.data)
                for(let n=this.analyseData.length-1;n>=0;n--){
                    for(let m=0;m<res.data.length;m++){
                        if(this.analyseData[n].targetId === res.data[m].targetid){
                            this.analyseData[n].probability = res.data[m].probability
                            this.analyseData[n].type = "底事件"
                            break
                        }
                    }
                    // 考虑块-块连接的情况，直接传递概率
                    if(this.dataType(this.analyseData[n])===3){
                        console.log("块-块")
                        for(let i=0;i<this.analyseData.length;i++){
                            if(this.analyseData[i].targetId === this.analyseData[n].sourceId){
                                this.analyseData[i].probability = this.analyseData[n].probability
                                break
                            }
                        }
                    }
                }
                console.log(JSON.parse(JSON.stringify(this.analyseData)))
                //找到所有target是门的，一次门就是一次运算（先把概率都计算好）
                for(let n=this.analyseData.length-1;n>=0;n--){
                    if(this.analyseData[n].shape === "门"){
                        if(this.analyseData[n].target === "与门"){
                            this.analyseData[n].probability = 1
                            for(let m=0;m<this.analyseData.length;m++){
                                if(this.analyseData[m].sourceId === this.analyseData[n].targetId){
                                    this.analyseData[n].probability *= this.analyseData[m].probability
                                }
                            }
                            for(let i=0;i<this.analyseData.length;i++){
                                if(this.analyseData[i].targetId=== this.analyseData[n].sourceId){
                                    this.analyseData[i].probability = this.analyseData[n].probability
                                }
                            }
                        }
                        else if(this.analyseData[n].target === "或门"){
                            this.analyseData[n].probability = 1
                            for(let m=0;m<this.analyseData.length;m++){
                                if(this.analyseData[m].sourceId === this.analyseData[n].targetId){
                                    this.analyseData[n].probability *= 1-this.analyseData[m].probability
                                }
                            }
                            this.analyseData[n].probability = 1-this.analyseData[n].probability
                            for(let i=0;i<this.analyseData.length;i++){
                                if(this.analyseData[i].targetId=== this.analyseData[n].sourceId){
                                    this.analyseData[i].probability = this.analyseData[n].probability
                                }
                            }
                        }
                    }
                }
                console.log(JSON.parse(JSON.stringify(this.analyseData)))
                for(let k=0;k<this.analyseData.length;k++){
                    this.analyseData[k].important = this.analyseData[k].probability
                }
                // 计算所有底事件相对于选定的顶事件的概率重要度
                for (let j = 0; j < this.analyseData.length; j++) {
                    if (this.analyseData[j].type === "底事件") {
                        if (this.analyseData[j].source === "与门") {
                            this.analyseData[j].important = 1
                        } else if (this.analyseData[j].source === "或门") {
                            this.analyseData[j].important = 0
                        }
                        if (this.dataType(this.analyseData[j]) === 3) {
                            for (let i = 0; i < this.analyseData.length; i++) {
                                if (this.analyseData[i].targetId === this.analyseData[j].sourceId) {
                                    if (this.analyseData[i].source === "与门") {
                                        this.analyseData[i].important = 1
                                    } else if (this.analyseData[i].source === "或门") {
                                        this.analyseData[i].important = 0
                                    } else {
                                        this.analyseData[i].important = this.analyseData[j].important
                                    }
                                    break
                                }
                            }
                        }
                        // 将该底事件相关的所有门都进行标记
                        let sourceId = this.analyseData[j].sourceId
                        for(let k= this.analyseData.length - 1;k>=0;k--){
                            this.analyseData[k].importantId = 0
                            if(this.analyseData[k].targetId === sourceId){
                                this.analyseData[k].importantId = 1
                                sourceId = this.analyseData[k].sourceId
                            }
                        }
                        for (let n = this.analyseData.length - 1; n >= 0; n--) {
                            if (this.analyseData[n].shape === "门") {
                                // 与门没影响
                                if (this.analyseData[n].target === "与门") {
                                    this.analyseData[n].important = 1
                                    for (let m = 0; m < this.analyseData.length; m++) {
                                        if (this.analyseData[m].sourceId === this.analyseData[n].targetId) {
                                            this.analyseData[n].important *= this.analyseData[m].important
                                        }
                                    }
                                    for (let i = 0; i < this.analyseData.length; i++) {
                                        if (this.analyseData[i].targetId === this.analyseData[n].sourceId) {
                                            this.analyseData[i].important = this.analyseData[n].important
                                            // 如果继承了该important的块-门所对应的上一个门是或门就要重新计算
                                            if (this.analyseData[n].importantId === 1 && this.analyseData[i].source === "或门") {
                                                console.log("继承改变")
                                                this.analyseData[i].important = 1 - this.analyseData[i].important
                                                break
                                            }
                                        }
                                    }
                                }
                                // 或门要区分importantId
                                else if (this.analyseData[n].target === "或门") {
                                    this.analyseData[n].important = 1
                                    for (let m = 0; m < this.analyseData.length; m++) {
                                        if (this.analyseData[m].sourceId === this.analyseData[n].targetId) {
                                            this.analyseData[n].important *= 1 - this.analyseData[m].important
                                        }
                                    }
                                    if(this.analyseData[n].importantId === 0){
                                        this.analyseData[n].important = 1-this.analyseData[n].important
                                    }
                                    for (let i = 0; i < this.analyseData.length; i++) {
                                        if (this.analyseData[i].targetId === this.analyseData[n].sourceId) {
                                            this.analyseData[i].important = this.analyseData[n].important
                                            // 如果继承了该important的块-门所对应的上一个门是或门就要重新计算
                                            if (this.analyseData[n].importantId === 1 && this.analyseData[i].source === "或门") {
                                                console.log("继承改变")
                                                this.analyseData[i].important = 1 - this.analyseData[i].important
                                                break
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        // 储存当前对应的important
                        this.analyseData[j].importantnum = this.analyseData[0].important
                        for (let k = 0; k < this.analyseData.length; k++) {
                            this.analyseData[k].important = this.analyseData[k].probability
                        }
                    }
                }
                // 显示
                console.log(JSON.parse(JSON.stringify(this.analyseData)))
                for(let i=0;i < this.analyseData.length; i++){
                    if(this.analyseData[i].type === "底事件"){
                        console.log(this.analyseData[i].target)
                        console.log(this.analyseData[i].importantnum)
                    }
                }
                for(let i=0;i < this.analyseData.length; i++) {
                    if (this.analyseData[i].shape === "门") {
                        console.log(this.analyseData[i].source)
                        console.log(this.analyseData[i].probability)
                    }
                }
                // 概率重要度显示表格
                this.importantData = this.analyseData.map(function (item) {
                    if(item.type === "底事件"){
                        return item
                    }
                });
                this.importantData = this.importantData.filter(function (e) {
                    return e
                });
                console.log(JSON.parse(JSON.stringify(this.importantData)))
                this.importantDialogVisible = true
            },
            // 判断edge连接的数据类型
            dataType(data){
                // 块-门
                if(this.edgeType(data.source) === false && this.edgeType(data.target) === true)
                    return 1
                // 门-块
                else if(this.edgeType(data.source) === true && this.edgeType(data.target) === false)
                    return 2
                else return 3
            },
            // 判断是否为门
            edgeType(data){
                return (data === "与门" || data === "或门" || data === "禁门" || data === "非门" || data === "转移门");
            },
            // input改变事件
            change() {
                this.$forceUpdate()
            },
            importantDialogClosed() {
            },
            importantDialogInfo(){
                this.importantDialogVisible = false
            },
            targetDialogClosed() {
            },
            targetDialogInfo(){
                this.targetDialogVisible = false
            },
        }
    }
</script>

<style scoped>
    .sider {
        position: relative;
        width: 0px;
        height: 0px;
        display: none;
    }
</style>
